﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace DevicesView.Logic
{

    public static class Tool
    {
        public static void ObjectSaveToXML(object obj, string xmlpath)
        {
            if (obj == null && File.Exists(xmlpath))
            {
                File.Delete(xmlpath);
                return;
            }
            string path = Path.GetDirectoryName(Path.GetFullPath(xmlpath));
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            using (FileStream fs = File.Create(xmlpath))
            {
                XmlSerializer xml = new XmlSerializer(obj.GetType());
                xml.Serialize(fs, obj);
            }
        }
        public static T GetObjectByXML<T>(string xmlpath)
        {
            if (!File.Exists(xmlpath))
                return default(T);
            try
            {
                using (FileStream fs = File.Open(xmlpath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XmlSerializer xml = new XmlSerializer(typeof(T));
                    return (T)xml.Deserialize(fs);
                }
            }
            catch (Exception ex)
            {
                BCL.BaseLib.Basics.BCLApplication.log?.Error(ex);
                return default(T);
            }
        }

        public static void ObjectSaveToFile(object obj, string filePath)
        {
            using (FileStream fs = File.Create(filePath))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter format = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                format.Serialize(fs, obj);
            }
        }
        public static T GetObjectByFile<T>(string filePath)
        {
            if (!File.Exists(filePath))
                return default;
            using (FileStream fs = File.OpenRead(filePath))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter format = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                return (T)format.Deserialize(fs);
            }
        }


        public static T GetObjectByClass<T>(string dllName, string typeName)
        {
            string dll = AppDomain.CurrentDomain.BaseDirectory + dllName;
            if (!File.Exists(dll))
                return default(T);
            try
            {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(dll);
                return (T)assembly.CreateInstance(typeName);
            }
            catch (Exception ex)
            {
                BCL.BaseLib.Basics.BCLApplication.log.Error(ex);
            }
            return default(T);
        }

        public static byte[] GetBytes(this string str, System.Text.Encoding encoder)
        {
            return encoder.GetBytes(str);
        }

        public static double GetNum(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return 0;
            if (str.Trim().Length == 0)
                return 0;
            string newstr = Regex.Replace(str, @"[^\d.\d]", "");
            double.TryParse(newstr, out double num);
            return num;
        }
    }
}
